#
# Author: pepone.onrez@gmail.com
# License: GPL-v3
#
# This cmake script generate rules for build antlr c++ based lexers
# This script is tested with antlr-2 version as there is not yet a c++ runtime for antlr-3
#
# Assumptions all grammars are in the antlr source dir and end in .g (This is standar file extension for antlr)
# The same file has lexer and parser definitions.
# The macro permit you get the name of the generated files for the lexer.
# You can use this names for build other rules.
#
# If you has any suggestions fell free to post in the user group
# http://groups.google.com/group/ydra
#


# TODO change this for use FindAntlr
SET ( ANTLR_COMMAND /opt/antlr/bin/antlr )

# Generate rules for antlr lexers

# This macro returns the names of the sources that's generate antlr for a given lexer and generate
# a command that depends on the given lexer and outputs the lexer source files runing the antlr compiler.
MACRO ( GENERATE_LEXER_RULES LEXER_GENERATED_SOURCE LEXER_GENERATED_HEADER LEXER_GENERATED_TOKENS_HEADER LEXER_GENERATED_TOKENS_TXT )

    # FileName for our lexer definition
    SET ( LEXER_ANTRL_SOURCE_BASENAME ${ARGN} )

    # Full path for our lexer definition
    SET ( LEXER_ANTRL_SOURCE_FILE ${PROJECT_SOURCE_DIR}/antlr/${LEXER_ANTRL_SOURCE_BASENAME} )

    #Generate filenames based on the lexer filename
    STRING( REGEX REPLACE "\\.g" .cpp LEXER_CPP_SOURCE_BASENAME "${LEXER_ANTRL_SOURCE_BASENAME}" )
    STRING( REGEX REPLACE "\\.g" .hpp LEXER_CPP_HEADER_BASENAME "${LEXER_ANTRL_SOURCE_BASENAME}" )
    STRING( REGEX REPLACE "\\Lexer.g" TokenTypes.hpp LEXER_TOKENS_CPP_HEADER_BASENAME "${LEXER_ANTRL_SOURCE_BASENAME}" )
    STRING( REGEX REPLACE "\\Lexer.g" TokenTypes.txt LEXER_TXT_TOKENS_BASENAME "${LEXER_ANTRL_SOURCE_BASENAME}" )

    #Files generated by antlr
    SET ( LEXER_CPP_SOURCE_FILE ${PROJECT_BINARY_DIR}/${LEXER_CPP_SOURCE_BASENAME} )
    SET ( LEXER_CPP_HEADER_FILE ${PROJECT_BINARY_DIR}/${LEXER_CPP_HEADER_BASENAME} )
    SET ( LEXER_TOKENS_CPP_HEADER_FILE ${PROJECT_BINARY_DIR}/${LEXER_TOKENS_CPP_HEADER_BASENAME} )
    SET ( LEXER_TXT_TOKENS_FILE ${PROJECT_BINARY_DIR}/${LEXER_TXT_TOKENS_BASENAME} )


    #Command that runs antlr for generating the lexer code
    ADD_CUSTOM_COMMAND(
        OUTPUT ${LEXER_CPP_SOURCE_FILE} ${LEXER_CPP_HEADER_FILE} ${LEXER_TOKENS_CPP_HEADER_FILE} ${LEXER_TXT_TOKENS_FILE}
        COMMAND ${ANTLR_COMMAND} ${LEXER_ANTRL_SOURCE_FILE}
        DEPENDS ${LEXER_ANTRL_SOURCE_FILE}
        COMMENT "-- Generating ${LEXER_CPP_SOURCE_FILE} ${LEXER_CPP_HEADER_FILE} ${LEXER_TOKENS_CPP_HEADER_FILE} ${LEXER_TXT_TOKENS_FILE} file from ${LEXER_ANTRL_SOURCE_BASENAME}")


    # Asign return values
    SET( LEXER_GENERATED_SOURCE ${LEXER_CPP_SOURCE_FILE} )
    SET( LEXER_GENERATED_HEADER ${LEXER_CPP_HEADER_FILE} )
    SET( LEXER_GENERATED_TOKENS_HEADER ${LEXER_TOKENS_CPP_HEADER_FILE} )
    SET( LEXER_GENERATED_TOKENS_TXT ${LEXER_TXT_TOKENS_FILE} )

ENDMACRO ( GENERATE_LEXER_RULES LEXER_GENERATED_SOURCE LEXER_GENERATED_HEADER LEXER_GENERATED_TOKENS_HEADER LEXER_GENERATED_TOKENS_TXT )
